//
// Copyright (C) 2008 Jackson Harper (jackson@ximian.com)

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//

using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web.Security;

using log4net;
using MySql.Data.MySqlClient;

namespace Grr.Models
{

	public class People
	{
		private static readonly ILog log = LogManager.GetLogger(typeof(People));

		public static string UrlForPerson(string person)
		{
			return String.Concat("~/people/", StringUtil.ToFriendlyUri(person));
		}

		public static bool CreateUser(string username, string email, string password, string location, out string error)
		{
			MembershipUser user = null;
			try
			{
			   user = Membership.CreateUser(username, password, email);
			}
			catch (MembershipCreateUserException mce)
			{
				error = mce.Message;
				log.ErrorFormat ("Unable to create user: {0}  username:{1}, email:{2}, location:{3}", error, username, email, location);
				return false;
			}

			if (location != null && location.Length > 0)
				CreateUserProfile((long)user.ProviderUserKey, location);

			error = null;
			return true;
		}

		public static void CreateUserProfile(long user_id, string location)
		{
			using (MySqlConnection con = DbContext.OpenReadWriteConnection ())
			{
				MySqlCommand cmd = new MySqlCommand("INSERT INTO UserProfile (user_id, location) VALUES (?user_id, ?location)", con);
				cmd.Parameters.Add("?user_id", MySqlDbType.Int64).Value = user_id;
				cmd.Parameters.Add("?location", MySqlDbType.VarChar, Validator.MaxLocationLength).Value = location;

				if (cmd.ExecuteNonQuery() != 1)
				{
					log.ErrorFormat("Unable to INSERT INTO UserProfile in CreateUserProfile for user: {0}  location: {1}", user_id, location);
				}
			}
		}

		public static void GetResultsForPerson(long user_id, int max, out SearchResult created, out SearchResult doing, out SearchResult want_to_do)
		{
			created = GetGrurrahsCreatedByUser(user_id, max, 0);
			doing = GetGrurrahsUserDoing (user_id, max, 0);
			want_to_do = GetGrurrahsUserWantsToDo(user_id, max, 0);
		}

		public static SearchResult GetGrurrahsCreatedByUser(long user_id, int max_results, int start_index)
		{
			List<SearchMatch> matches = new List<SearchMatch>();
			bool more_available = false;
			using (MySqlConnection rcon = DbContext.OpenReadConnection())
			{
				MySqlCommand qcmd = new MySqlCommand("SELECT Grurrahs.name FROM Grurrahs WHERE Grurrahs.creator = ?user_id LIMIT ?start_index,?max_results", rcon);
				qcmd.Parameters.Add (new MySqlParameter ("?user_id", MySqlDbType.Int64)).Value = user_id;
				qcmd.Parameters.Add("?max_results", MySqlDbType.Int32).Value = max_results + 1;
				qcmd.Parameters.Add("?start_index", MySqlDbType.Int32).Value = start_index;
				MySqlDataReader reader = qcmd.ExecuteReader();

				int i = 0;
				while (reader.Read())
				{
					string title = reader.GetString (0);
					SearchMatch match = new SearchMatch (title, String.Empty, Grurrah.UrlForGrurrah (title), i++);
					matches.Add (match);
				}

				more_available = matches.Count > max_results;
				if (more_available)
					matches.RemoveAt(max_results);

				return new SearchResult (matches, more_available);
			}
		}

		public static SearchResult GetGrurrahsUserDoing(long user_id, int max, int start_index)
		{
			return SearchResult.Empty;
		}

		public static SearchResult GetGrurrahsUserWantsToDo (long user_id, int max, int start_index)
		{
			return SearchResult.Empty;
		}
	}
}

